﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WindowsFormsApplication3;

namespace bsy
{
    public partial class Setup : Form
    {
        public Setup()
        {
            InitializeComponent();
        }
        private void Setup_Load(object sender, EventArgs e)
        {
            show_shop_info();
            show_deed_info();
        }

        private void show_deed_info()
        {
            try
            {
                string sql = string.Format("select min 左区间,max 右区间,scale 提成 from deedsinterzone");
                DataSet ds = new DataSet();
                ds = CommonDB.Query(sql);
                this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                ds.Dispose();
               
            }
            catch (Exception e1)
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

        }



        private void show_shop_info()
        {

            //获取店铺信息
            try
            {
                string sql = "SELECT * FROM shop";
                DataSet ds = new DataSet();
                ds = CommonDB.Query(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    tb_shopname.Text = ds.Tables[0].Rows[0][0].ToString();
                    tb_shopaddr.Text = ds.Tables[0].Rows[0][1].ToString();
                    tb_shopphone.Text = ds.Tables[0].Rows[0][2].ToString();
                    tb_shopboss.Text = ds.Tables[0].Rows[0][3].ToString();
                    tb_shopre.Text = ds.Tables[0].Rows[0][4].ToString();
                }
                ds.Dispose();
            }
            catch
            {

                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        //保存店铺信息
        private void update_Click(object sender, EventArgs e)
        {
         try
            {
                string name = tb_shopname.Text.Trim();
                string address = tb_shopaddr.Text.Trim();
                string phone = tb_shopphone.Text.Trim();
                string boss = tb_shopboss.Text.Trim();
                string remark = tb_shopre.Text.Trim();
                string sql = string.Format("update shop set name='{0}', address='{1}', phone='{2}', boss='{3}', remark='{4}'", name, address, phone, boss, remark);
                int temp;
                temp = CommonDB.ExecuteSql(sql);
                if (temp == 1)
                {
                    MessageBox.Show("保存成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        //清除
        private void bt_deedsdel_Click(object sender, EventArgs e)
        {
         try
            {
                int line = dataGridView1.Rows.Count; //最大行
                if (line > 0)
                {
                    string sql = string.Format("delete from deedsinterzone");
                    CommonDB.ExecuteSql(sql);
                    MessageBox.Show("清除成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    dataGridView1.DataSource = null;
                    show_deed_info();
                    return;
                }
            }
            catch
            {
                MessageBox.Show("清除失败，请重新操作", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        }

        //添加区间
        private void bt_deedsadd_Click(object sender, EventArgs e)
        {
        try
            {
                double max=0;
                double min=0;
                float scale=0;
                if (string.Empty == tb_deedsmin.Text || string.Empty == tb_deedsmax.Text || string.Empty == tb_deedsscale.Text)
                {
                    MessageBox.Show("左右区间和提成不能为空", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                try
                {
                    min = double.Parse(tb_deedsmin.Text.Trim());
                    max = double.Parse(tb_deedsmax.Text.Trim());
                    scale = float.Parse(tb_deedsscale.Text.Trim());
                }
                catch {
                    MessageBox.Show("请输入正确的数据格式", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
              
                int line = dataGridView1.Rows.Count; //最大行
                string sql;
                if (min < max)
                {
                    if (line == 0)
                    {
                        if (min != 0)
                        {
                            MessageBox.Show("最小值必须为0", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;

                        }
                        else
                        {
                            sql = string.Format("insert into deedsinterzone(min,max,scale)values('{0}','{1}','{2}')", min, max, scale);
                            int i = CommonDB.ExecuteSql(sql);
                            if (i == 1)
                            {
                                MessageBox.Show("添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                dataGridView1.DataSource = null;
                                show_deed_info();
                                return;

                            }
                            else
                            {
                                MessageBox.Show("添加失败，请重新操作", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return;
                            }
                        }
                    }

                    sql = string.Format("select MAX(max) from deedsinterzone"); //获取最大区间值
                    object o = CommonDB.GetSingle(sql);
                    double compare_max = 0;
                    if (null != o)
                    {
                        compare_max = double.Parse(o.ToString());
                    }
                    if (min == compare_max)
                    {

                        sql = string.Format("insert into deedsinterzone(min,max,scale)values('{0}','{1}','{2}')", min, max, scale);
                        int i = CommonDB.ExecuteSql(sql);
                        if (i == 1)
                        {
                            MessageBox.Show("添加成功", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            dataGridView1.DataSource = null;
                            show_deed_info();
                            return;
                        }
                        else
                        {
                            MessageBox.Show("添加失败，请重新操作", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            return;
                        }
                    }
                    else
                    {
                        MessageBox.Show("你输入的左区间数没有等于上一行的右区间数", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("请使左区间小于右区间", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }
            catch
            {
                MessageBox.Show("数据库出错", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //return;
                Setup deeds = new Setup();
                deeds.MdiParent = this.MdiParent;
                deeds.Show();
                this.Close();
            }
        }

      
    }
}
